package com.scilvet.reportcomposer.report;

import java.io.InputStream;

import org.apache.log4j.Logger;

import com.scilvet.reportcomposer.exception.ReportException;
import com.scilvet.reportcomposer.report.parser.ParseException;
import com.scilvet.reportcomposer.report.parser.ReportParser;

public class FileReportFactory implements ReportFactory<InputStream> {

	private ReportParser reportParser = null;
	
	private Logger logger = Logger.getLogger(FileReportFactory.class);
	
	public ReportStore getReport(InputStream inputStream) throws ReportException {
		
		if (reportParser == null) {
			reportParser = new ReportParser(inputStream);
		} else {
			reportParser.ReInit(inputStream);
		}
		
		ReportStore report = null;
		try {
			report = reportParser.reportList();
		} catch (ParseException e) {
			logger.error(e.getMessage());
			throw new ReportException("Error parsing report file", e);
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new ReportException("Error parsing report file", e);
		}
		
		return report;
	}
}
